package com.zxs.domain.service.impl;

import com.alibaba.fastjson.JSON;
import com.zxs.common.entity.PageResult;
import com.zxs.common.entity.Result;
import com.zxs.domain.entity.dto.SubjectInfoDTO;
import com.zxs.domain.handel.subject.SubjectTypeHandel;
import com.zxs.domain.handel.subject.SubjectTypeHandelFactory;
import com.zxs.domain.service.SubjectInfoDomainService;
import com.zxs.infra.basic.entity.SubjectInfo;
import com.zxs.infra.basic.entity.SubjectMapping;
import com.zxs.infra.basic.mapper.SubjectInfoMapper;
import com.zxs.infra.basic.service.SubjectInfoService;
import com.zxs.infra.basic.service.SubjectMappingService;
import com.zxs.infra.basic.service.impl.SubjectInfoServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * 题目表(SubjectInfo)表服务实现类
 *
 * @author makejava
 * @since 2025-11-12 21:01:30
 */
@Service
public class SubjectInfoServiceDomainImpl implements SubjectInfoDomainService {
    private static final Logger log = LoggerFactory.getLogger(SubjectInfoServiceDomainImpl.class);
    @Autowired
    private SubjectInfoMapper subjectInfoMapper;

    @Autowired
    private SubjectTypeHandelFactory subjectTypeHandelFactory;

    @Autowired
    private SubjectMappingService subjectMappingService;
    @Autowired
    private SubjectInfoServiceImpl subjectInfoService;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override

    public SubjectInfo queryById(Integer id) {
        return this.subjectInfoMapper.queryById(id);
    }


    /**
     * 新增数据
     *
     * @param subjectInfo 实例对象
     * @return 实例对象
     */
    @Override
    public SubjectInfo insert(SubjectInfo subjectInfo) {
        this.subjectInfoMapper.insert(subjectInfo);
        return subjectInfo;
    }

    /**
     * 修改数据
     *
     * @param subjectInfo 实例对象
     * @return 实例对象
     */
    @Override
    public SubjectInfo update(SubjectInfo subjectInfo) {
        this.subjectInfoMapper.update(subjectInfo);
        return this.queryById(subjectInfo.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.subjectInfoMapper.deleteById(id) > 0;
    }

    /**
     * 新增题目
     *
     * @param subjectInfoDTO
     * @return
     */
    @Override
    public Boolean insertSubInfo(SubjectInfoDTO subjectInfoDTO) {

        log.info("新增题目接口的参数------------{}", JSON.toJSONString(subjectInfoDTO));

        //采用工厂+策略的形式进行开发
        //一个工厂包含  4 种类型   根据传入的类型自动映射选择处理
        SubjectInfo subjectInfo = new SubjectInfo();
        BeanUtils.copyProperties(subjectInfoDTO, subjectInfo);
        int insert = subjectInfoMapper.insert(subjectInfo);

        SubjectTypeHandel handel = subjectTypeHandelFactory.getHandel(subjectInfoDTO.getSubjectType());
        Result result = handel.add(subjectInfoDTO);

        //分类ids
        List<Integer> categoryIds = subjectInfoDTO.getCategoryIds();
        //标签ids
        List<Integer> labelIds = subjectInfoDTO.getLabelIds();

        List<SubjectMapping> subjectMappings = new ArrayList<>();

        categoryIds.forEach(categoryId -> {
            labelIds.forEach(labelId -> {
                SubjectMapping subjectMapping = new SubjectMapping();
                subjectMapping.setCategoryId(categoryId);
                subjectMapping.setSubjectId(subjectInfoDTO.getId());
                subjectMapping.setLabelId(labelId);
                subjectMappings.add(subjectMapping);
            });
        });

        subjectMappingService.batchInsert(subjectMappings);
        return result.getSuccess();
    }

    @Override
    public PageResult<SubjectInfoDTO> getSubjectPage(SubjectInfoDTO subjectInfoDTO) {
        PageResult<SubjectInfoDTO> result = new PageResult<>();

        result.setPageNo(subjectInfoDTO.getPageNo());
        result.setPageSize(subjectInfoDTO.getPageSize());
        int start = (subjectInfoDTO.getPageNo() - 1) * subjectInfoDTO.getPageSize();
        result.setStart(start);
        SubjectInfo subjectInfo = new SubjectInfo();
        BeanUtils.copyProperties(subjectInfoDTO,subjectInfo);
        int count = subjectInfoService.countByCondition(subjectInfoDTO.getCategoryId(),
                subjectInfoDTO.getLabelId(), subjectInfoDTO.getSubjectDifficult());
        result.setTotal(count);
        if (count == 0) {
            return result;
        }
        List<com.zxs.infra.basic.entity.dto.SubjectInfoDTO> subjectInfoDTOS = subjectInfoService.querySubjectInfoByPage(subjectInfo, subjectInfoDTO.getCategoryId(),
                subjectInfoDTO.getLabelId(), start, subjectInfoDTO.getPageSize());
        ArrayList<SubjectInfoDTO> infoDTOS = new ArrayList<>();
        BeanUtils.copyProperties(subjectInfoDTOS,infoDTOS);
        result.setRecords(infoDTOS);
        return result;
    }

    /**
     * 查询题目详情
     * @param subjectInfoDTO
     * @return
     */
    @Override
    public Result<SubjectInfoDTO> querySubjectInfo(SubjectInfoDTO subjectInfoDTO) {
        Result<SubjectInfoDTO> result = new Result<>();
        SubjectInfo subjectInfo = subjectInfoService.queryById(subjectInfoDTO.getSubjectId());
        Integer subjectType = subjectInfo.getSubjectType();
        SubjectTypeHandel handel = subjectTypeHandelFactory.getHandel(subjectType);

        SubjectInfoDTO subjectInfoDTO1 = handel.query(subjectInfoDTO.getSubjectId());
        subjectInfoDTO1.setSubjectParse(subjectInfo.getSubjectParse());
        subjectInfoDTO1.setSubjectName(subjectInfo.getSubjectName());
        subjectInfoDTO1.setSubjectDifficult(subjectInfo.getSubjectDifficult());
        result = Result.success("查询题目成功", subjectInfoDTO1);
        return result;
    }
}
